/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package quartz;

import infr.Logger4j;
import java.util.Properties;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;

/**
 *
 * @author bighead
 */
public class SchedulerFactoryManager {

    private static Scheduler scheduler;
    private static Properties schedulerProperties;
    private static SchedulerFactory sf;

    /**
     * @param args the command line arguments
     */
    static {
        try {

            //initSchedulerFactoryByCode();
            
            initStdSchedulerFactory();
        } catch (SchedulerException ex) {
            Logger4j.error("Init SchedulerFactory Failed with exception:" + ex.getMessage());
        }
    }

    private static void initSchedulerFactoryByCode() throws SchedulerException {
        schedulerProperties = new Properties();

        getSchedulerProperties().put("org.quartz.scheduler.instanceName", "EX_JobScheduler");
        getSchedulerProperties().put("org.quartz.scheduler.instanceId", "EX_Instance");
        getSchedulerProperties().put("org.quartz.scheduler.batchTriggerAcquisitionMaxCount", "5");
        getSchedulerProperties().put("org.quartz.scheduler.skipUpdateCheck", true);
        getSchedulerProperties().put("org.quartz.jobStore.class", "org.quartz.simpl.RAMJobStore");
        getSchedulerProperties().put("org.quartz.threadPool.class", "org.quartz.simpl.SimpleThreadPool");
        getSchedulerProperties().put("org.quartz.threadPool.threadCount", "5");// work threadpool
        getSchedulerProperties().put("org.quartz.threadPool.threadPriority", "5");// 1-10, higer priority thread will run first

        sf = new StdSchedulerFactory(getSchedulerProperties());
        if (!sf.getScheduler().isStarted()) {
            getSchedulerFactory().getScheduler().start();
        }

    }

    private static void initStdSchedulerFactory() throws SchedulerException {
        org.quartz.SchedulerFactory schedulerFactory = new StdSchedulerFactory();
        scheduler = schedulerFactory.getScheduler();
        getScheduler().start();
    }

    /**
     * @return the scheduler
     */
    public static Scheduler getScheduler() {
        return scheduler;
    }

    /**
     * @return the schedulerProperties
     */
    public static Properties getSchedulerProperties() {
        return schedulerProperties;
    }

    /**
     * @return the sf
     */
    public static SchedulerFactory getSchedulerFactory() {
        return sf;
    }

}
